package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum FanStatus implements EnumTrans<CompleteEntity.FanStatus, FanStatus> {
    /**
     * 0 风扇停止
     * 1 风扇正常运行
     * 2 风扇异常
     */
    STOPPED(CompleteEntity.FanStatus.STOPPED),
    RUNNING(CompleteEntity.FanStatus.RUNNING),
    ABNORMAL(CompleteEntity.FanStatus.ABNORMAL),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.FanStatus fanStatus;
    private static final long serialVersionUID = 987654321987654321L;

    FanStatus(CompleteEntity.FanStatus fanStatus) {
        this.id = fanStatus.getNumber();
        this.name = fanStatus.name();
        this.fanStatus = fanStatus;
    }

    /**
     * @param fanStatus
     * @return 对应的类型
     */
    public static FanStatus getFanStatusStruct(CompleteEntity.FanStatus fanStatus) {
        Objects.requireNonNull(fanStatus, "fanStatus can not be null.");
        return Arrays.stream(FanStatus.values())
                .filter(e -> e.name.equals(fanStatus.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.FanStatus toProto() {
        return this.fanStatus;
    }

    @Override
    public FanStatus formProto(CompleteEntity.FanStatus fanStatus) {
        return getFanStatusStruct(fanStatus);
    }
}

